package state.component;

import java.util.Vector;

import state.GameConstants;
import state.GameImage;
import state.Graphics;
import state.Manager;
import state.Sprite;
import state.font.ImageTextImpl;
import state.util.array.StringArray;
import iplayparticle.view.iplayparticleResources;

public class MenuItem extends Component {
  public Vector items = new Vector();
  public MenuItem parent = null;
  public GameMenu menu = null;

  public boolean isShowChildrenMenuItem = false;
  public int currentChooseIndex = -1;
  public boolean isShowUpToDown = false;
  public String label1 = "";

  public GameImage arowImage = null;

  public MenuItem(String label2) {
    super(null, 0, 0, GameConstants.SCREEN_WIDTH, GameConstants.SCREEN_HEIGHT);
    this.label1 = label2;
  }

  public void initComponent() {
    super.initComponent();
    setVisible(false);
  }

  public MenuItem getParent() {
    return parent;
  }

  public void setParent(MenuItem parent1) {
    this.parent = parent1;
  }

  public void setManager(Manager manager) {
    if (manager != null) {
      manager.append(this);
    }
  }

  public GameMenu getMenu() {
    return menu;
  }

  public void setMenu(GameMenu menu1) {
    this.menu = menu1;
    setWidth(menu1.itemWidth);
    setHeight(menu1.itemHeight);
    if (menu1._manager != null) {
      setManager(menu1._manager);
    }
    for (int i = 0; i < items.size(); i++) {
      ((MenuItem) items.elementAt(i)).setMenu(menu1);
    }
  }

  public void addItems(StringArray items1) {
    for (int i = 0; i < items1.len(); i++) {
      addItem(items1.array[i]);
    }
  }

  public boolean hasChild(String label2) {
    for (int i = 0; i < items.size(); i++) {
      MenuItem item = (MenuItem) items.elementAt(i);
      if (item.getLabel().equals(label2)) {
        return true;
      }
    }
    return false;
  }

  public void insertItem1(MenuItem item, int index) {
    if (menu != null) {
      item.setMenu(menu);
      if (menu.currentMenuItem == menu) {
        menu.currentMenuItem = item;
        menu.currentMenuItem.setFocused(true);
      }
    }
    item.setParent(this);
    items.insertElementAt(item, index);
    isShowUpToDown = checkDrawUpOrDown();
    for (int i = 0; i < items.size(); i++) {
      updateChildPosition((MenuItem) items.elementAt(i), i, isShowUpToDown);
    }
  }

  public MenuItem insertItem(String itemLabel, int index) {
    MenuItem item = new MenuItem(itemLabel);
    insertItem1(item, index);
    return item;
  }

  public MenuItem addItem(String itemLabel) {
    MenuItem item = new MenuItem(itemLabel);
    addItem1(item);
    return item;
  }

  public void addItem1(MenuItem item) {
    if (menu != null) {
      item.setMenu(menu);
      if (menu.currentMenuItem == menu) {
        menu.currentMenuItem = item;
        menu.currentMenuItem.setFocused(true);
      }
    }
    item.setParent(this);
    items.addElement(item);
    isShowUpToDown = checkDrawUpOrDown();
    updateChildPosition(item, items.size() - 1, isShowUpToDown);
  }

  public MenuItem getSubMenuByIndex(int index) {
    if (index > items.size() - 1) {
      return null;
    }
    return (MenuItem) items.elementAt(index);
  }

  public MenuItem getSubMenu(String itemLabel) {
    for (int i = 0; i < items.size(); i++) {
      MenuItem item = (MenuItem) items.elementAt(i);
      if (item.getLabel().equals(itemLabel)) {
        return item;
      }
    }
    return null;
  }

  public String getLabel() {
    return label1;
  }

  public void setLabel(String label2) {
    this.label1 = label2;
  }

  protected boolean checkDrawUpOrDown() {
    if (menu == null) {
      return false;
    }

    if (items.size() * menu.itemHeight > GameConstants.SCREEN_HEIGHT - menu.menuY) {
      return false;
    } else {
      return true;
    }
  }

  public void removeItem(MenuItem item) {
    items.removeElement(item);
  }

  public void removeAll() {
    items.removeAllElements();
  }

  public MenuItem removeItemByIndex(int index) {
    if ((index < 0) || (index >= items.size())) {
      System.out.println("MenuItem : removeItem : index is invalid: " + index);
      return null;
    }
    
    MenuItem item = (MenuItem) items.elementAt(index);
    items.removeElementAt(index);
    return item;
  }

  public MenuItem removeItemByLabel(String itemLabel) {
    for (int i = 0; i < items.size(); i++) {
      MenuItem item = (MenuItem) items.elementAt(i);
      if (itemLabel.equals(item.getLabel())) {
        items.removeElement(item);
        return item;
      }
    }
    return null;
  }

  public void setFont(ImageTextImpl font) {
    super.setFont(font);
    for (int i = 0; i < items.size(); i++) {
      ((MenuItem) items.elementAt(i)).setFont(font);
    }
  }

  public void showChildMenu() {
    if (!isShowChildrenMenuItem && (items.size() > 0)) {
      isShowChildrenMenuItem = true;
      for (int i = 0; i < items.size(); i++) {
        MenuItem menuItem = (MenuItem) items.elementAt(i);
        menuItem.setVisible(true);
      }
      menu.currentMenuItem = ((MenuItem) items.elementAt(0));
      menu.currentMenuItem.setFocused(true);
      currentChooseIndex = 0;
    }
  }

  public void hideChildMenu() {
    if (isShowChildrenMenuItem) {
      isShowChildrenMenuItem = false;
      currentChooseIndex = -1;
      setFocused(true);
      if (menu != null) {
        menu.currentMenuItem = this;
      }

      for (int i = 0; i < items.size(); i++) {
        MenuItem menuItem = (MenuItem) items.elementAt(i);
        menuItem.hideChildMenu();
        menuItem.setVisible(false);
        menuItem.setFocused(false);
      }
    }
  }

  public void updateChildPosition(MenuItem item, int itemIndex, boolean isShowUpToDown1) {
    if (menu == null) {
      return;
    }

    int x1 = getMenuX() + menu.itemWidth + 2;
    if (!menu.openChildMenuToRight) {
      x1 = getMenuX() - menu.itemWidth - 2;
    }

    int y1 = 0;
    if (isShowUpToDown1) {
      y1 = getMenuY() + itemIndex * (menu.itemHeight - 1) + menu.textDy;
    } else {
      y1 = getMenuY() - (items.size() - itemIndex - 1) * (menu.itemHeight - 1) - menu.textDy;
    }
    item.setPosition(x1, y1);
  }

  public void draw(Graphics g) {
    if (menu == null) {
      return;
    }

    if (!parent.isShowChildrenMenuItem) {
      return;
    }

    if (menu._fillBackGround) {
      g.setColor(menu._backgroundColor);
      if (_manager == null) {
        g.fillRect(_realX, _realY, menu.itemWidth, menu.itemHeight);
      } else {
        g.fillRect(_realX, _realY, menu.itemWidth - 1, menu.itemHeight);
      }
    }

    if (menu.fillFocusItemBg && _focused) {
      g.setColor(menu._focusBgColor);
      if (_manager == null) {
        g.fillRect(_realX, _realY, menu.itemWidth, menu.itemHeight);
      } else {
        g.fillRect(_realX, _realY, menu.itemWidth - 1, menu.itemHeight);
      }
    }

    if (menu.itemBackgroundImage != null) {
      g.drawImage(menu.itemBackgroundImage, _realX, _realY, GameConstants.TOP_LEFT_ANCHOR);
    }

    if (menu._hasBorder) {
      g.setColor(menu.borderColor);
      if (_manager == null) {
        g.drawRect(_realX, _realY, menu.itemWidth - 1, menu.itemHeight - 1);
      } else {
        g.drawRect(_realX, _realY, menu.itemWidth - 2, menu.itemHeight - 1);
      }
    }

    if (_focused) {
      getFont().drawString2(g, label1, menu._focusFgColor, _realX + 3 + menu.textDx, _realY + menu.itemHeight / 2, GameConstants.VCENTER_LEFT_ANCHOR);
    } else {
      getFont().drawString2(g, label1, menu._forceGroundColor, _realX + 3 + menu.textDx, _realY + menu.itemHeight / 2, GameConstants.VCENTER_LEFT_ANCHOR);
    }

    if (items.size() > 0) {
      if (arowImage == null) {
        arowImage = new GameImage(iplayparticleResources.image_Muiten_trang, null, null).setStatic(true);
      }
      g.drawImage(arowImage, _realX + menu.itemWidth - 6, _realY + menu.itemHeight / 2, GameConstants.VCENTER_RIGHT_ANCHOR);
    }

    if ((items.size() > 0) && isShowChildrenMenuItem) {
      isShowUpToDown = checkDrawUpOrDown();
      for (int i = 0; i < items.size(); i++) {
        MenuItem item = (MenuItem) items.elementAt(i);
        updateChildPosition(item, i, isShowUpToDown);
        item.draw(g);
      }
    }
  }

  public void pointerReleased(Sprite source, int x1, int y1) {
    if (contains(x1, y1)) {
      if ((menu.getBottomBar() != null) && (menu.getBottomBar().getManager() != null)) {
        menu.getBottomBar().getManager().stopEvent();
      } else if (menu.getManager() != null) {
        menu.getManager().stopEvent();
      }

      if (items.size() > 0) {
        showChildMenu();
      } else {
        if (menu.listener != null) {
          menu.currentMenuItem.setFocused(false);
          menu.currentMenuItem = this;
          setFocused(true);
          changeIndexTo(this);

          menu.listener.onActionPerform(new Event(this, label1));
          menu.hide();
        }
      }
    } else {
      if (items.size() > 0) {
        for (int i = 0; i < items.size(); i++) {
          MenuItem item = (MenuItem) items.elementAt(i);
          item.pointerReleased(item, x1, y1);
        }
      }
    }
  }

  public void changeIndexTo(MenuItem menuItem) {
    for (int i = 0; i < items.size(); i++) {
      MenuItem item = (MenuItem) items.elementAt(i);
      if (items.elementAt(i) == menuItem) {
        currentChooseIndex = i;
        item.setFocused(true);
      } else {
        item.setFocused(false);
      }
    }
  }

  public void changeIndexToIndex(int index) {
    for (int i = 0; i < items.size(); i++) {
      MenuItem item = (MenuItem) items.elementAt(i);
      if (i == index) {
        currentChooseIndex = i;
        item.setFocused(true);
      } else {
        item.setFocused(false);
      }
    }
  }

  public int getMenuX() {
    return _realX;
  }

  public int getMenuY() {
    return _realY;
  }

  public Sprite detroy() {
    super.detroy();
    items = null;
    menu = null;
    label1 = null;
    parent = null;
    arowImage = null;
    return this;
  }
}
